import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('@/views/login/LoginPage.vue') }, // 登录页
    {
      path: '/',
      component: () => import('@/views/layout/LayoutContainer.vue'),
      redirect: '/article/manage',
      children: [
        {
          path: '/article/manage',
          component: () => import('@/views/article/ArticleManage.vue')
        },
        {
          path: '/article/channel',
          component: () => import('@/views/article/ArticleChannel.vue')
        },
        {
          path: '/user/profile',
          component: () => import('@/views/user/UserProfile.vue')
        },
        {
          path: '/user/avatar',
          component: () => import('@/views/user/UserAvatar.vue')
        },
        {
          path: '/user/password',
          component: () => import('@/views/user/UserPassword.vue')
        }
      ]
    }
  ]
})
// 登录访问拦截
// 定义一个数组, 专门存放所有不需要权限的页面
const noAuthUrls = ['/login']
// eslint-disable-next-line no-unused-vars
router.beforeEach((to, from) => {
  // 如果没有token,且访问的是非登录页，拦截到登录，其他情况正常方向
  const useStore = useUserStore()
  // console.log(to)
  if (!useStore.token && !noAuthUrls.includes(to.path)) return '/login'
  return true
})
export default router
